//
//  NoticeManager.m
//  特种车调度
//
//  Created by 陈宇 on 15/9/10.
//
//

#import "NoticeManager.h"
#import "BMAccountManager.h"
#import "ZDOrder.h"

@interface NoticeManager ()

@property (nonatomic, strong) BMPage *page1;    //消息
@property (nonatomic, strong) BMPage *page2;    //通知

@end

@implementation NoticeManager

- (instancetype)init
{
    if (self = [super init]) {
        _page1 = [BMPage defaultPage];
        _page2 = [BMPage defaultPage];
    }
    return self;
}

- (void)getMessageComplete:(completeListener)listener
{
    NSMutableDictionary *params = _page1.nextPage;
    if (!params) {
        listener(nil, [NSError errorWithDomain:kBMErrorDomain code:-1 userInfo:@{@"msg": @"已经到了最后一页，没有更多数据了。"}]);
        return;
    }
    //        params[@"sentUser"] = kRoleType ? [BMAccountManager sharedInstance].user.disId : [BMAccountManager sharedInstance].user.ownId;
    params[@"sentUser"] = @([BMAccountManager sharedInstance].user.ID);
    params[@"sentObject"] = @(kRoleType + 1);
    
    BMRequest *request = [BMRequest requestWithPath:@"getMySentLogs.htm" contentKey:@"msgs"];
    request.params = params;
    [Notice postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        _page1 = response.page;
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)deleteMsg:(Notice *)notice complete:(completeListener)listener
{
    BMRequest *request = [BMRequest requestWithPath:@"sentLog/delete.htm"];
    request.params = @{@"id": @(notice.ID)};
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)getNoticeComplete:(completeListener)listener
{
    NSMutableDictionary *params = _page2.nextPage;
    if (!params) {
        listener(nil, [NSError errorWithDomain:kBMErrorDomain code:-1 userInfo:@{@"msg": @"已经到了最后一页，没有更多数据了。"}]);
        return;
    }
    
    params[@"sentUser"] = kRoleType ? [BMAccountManager sharedInstance].user.disId : [BMAccountManager sharedInstance].user.ownId;
    
    params[@"sentObject"] = @(kRoleType + 1);
    
    BMRequest *request = [BMRequest requestWithPath:@"getAnnouncements.htm" contentKey:@"announcements"];
    request.params = params;
    [Notice postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        _page2 = response.page;
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)deleteNotice:(Notice *)notice complete:(completeListener)listener
{
    BMRequest *request = [BMRequest requestWithPath:@"announcement/delete.htm"];
    request.params = @{@"id": @(notice.ID)};
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        if (listener) {
            listener(response, error);
        }
    }];
}

- (void)resetWithType:(NSInteger)type
{
    switch (type) {
        case 1:
            _page1 = [BMPage defaultPage];
            break;
        case 2:
            _page2 = [BMPage defaultPage];
            break;
    }
}

@end
